package org.duanyu.shopping_user_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.duanyu.shopping_common.pojo.ShoppingUser;
import org.duanyu.shopping_common.result.BusException;
import org.duanyu.shopping_common.result.CodeEnum;
import org.duanyu.shopping_common.service.ShoppingUserService;
import org.duanyu.shopping_common.util.Md5Util;
import org.duanyu.shopping_user_service.util.JwtUtils;
import org.duanyu.shopping_user_service.mapper.ShoppingUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@DubboService
@Transactional
public class ShoppingUserServiceImpl implements ShoppingUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ShoppingUserMapper shoppingUserMapper;

    @Override
    public void saveRegisterCheckCode(String phone, String checkCode) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // redis中保存的建为手机号，值为验证码，过期时间5分钟
        valueOperations.set("registerCode:"+phone,checkCode,300, TimeUnit.SECONDS);
    }


    @Override
    public void registerCheckCode(String phone, String checkCode) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object checkCodeRedis = valueOperations.get("registerCode:" + phone);
        if (!checkCode.equals(checkCodeRedis)){
            throw new BusException(CodeEnum.CODE_ERROR);
        }
    }

    @Override
    public void register(ShoppingUser shoppingUser) {
        // 1.验证手机号是否存在
        String phone = shoppingUser.getPhone();
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        List<ShoppingUser> shoppingUsers = shoppingUserMapper.selectList(queryWrapper);
        if (shoppingUsers != null && !shoppingUsers.isEmpty()){
            throw new BusException(CodeEnum.PHONE_HAS_ALIVE);
        }
        // 2.验证用户名是否存在
        String username = shoppingUser.getUsername();
        QueryWrapper<ShoppingUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("username",username);
        List<ShoppingUser> shoppingUsers1 = shoppingUserMapper.selectList(queryWrapper1);
        if (shoppingUsers1 != null && !shoppingUsers1.isEmpty()){
            throw new BusException(CodeEnum.USERNAME_ALIVE);
        }
        // 3.新增用户
        shoppingUser.setStatus("Y");
        shoppingUser.setPassword(Md5Util.encode(shoppingUser.getPassword()));
        shoppingUserMapper.insert(shoppingUser);
    }

    @Override
    public String loginPassword(String username, String password) {
        // 1.验证用户名
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        if (shoppingUser == null){
            throw new BusException(CodeEnum.USERNAME_OR_PASSWORD_ERROR);
        }
        // 2.验证密码
        boolean verify = Md5Util.verify(password, shoppingUser.getPassword());
        if (!verify){
            throw new BusException(CodeEnum.USERNAME_OR_PASSWORD_ERROR);
        }
        // 3.生成JWT令牌，返回令牌
        return JwtUtils.sign(shoppingUser.getId(), shoppingUser.getUsername());
    }

    @Override
    public void saveLoginCheckCode(String phone, String checkCode) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // redis的键为手机号，值为验证码，过期时间5分钟
        valueOperations.set("loginCode:"+phone,checkCode,300,TimeUnit.SECONDS);
    }

    @Override
    public String loginCheckCode(String phone, String checkCode) {
        // 1.验证用户传入的手机号验证码是否在redis中存在
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object checkCodeRedis = valueOperations.get("loginCode:" + phone);
        if (!checkCode.equals(checkCodeRedis)){
            throw new BusException(CodeEnum.CODE_ERROR);
        }

        // 2.登录成功，根据手机号查询用户
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        // 3.生成JWT令牌，返回令牌
        return JwtUtils.sign(shoppingUser.getId(), shoppingUser.getUsername());
    }

    @Override
    public String getName(String token) {
        Map<String, Object> verify = JwtUtils.verify(token);
        return (String) verify.get("username");
    }

    @Override
    public ShoppingUser getLoginUser(String token) {
        // 拿到令牌中的用户id
        Map<String, Object> verify = JwtUtils.verify(token);
        Long userId = (Long) verify.get("userId");
        // 根据id查询用户
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return shoppingUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void checkPhone(String phone) {
        // 1.判断手机号是否存在
        QueryWrapper<ShoppingUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        if (shoppingUser == null){
            throw new BusException(CodeEnum.CODE_ERROR);
        }
        // 2.判断用户状态是否正常
        if (!"Y".equals(shoppingUser.getStatus())){
            throw new BusException(CodeEnum.LOGIN_USER_STATUS_ERROR);
        }
    }
}
